package com.googlecode.animeclassifier.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.googlecode.animeclassifier.Anime;
import com.googlecode.animeclassifier.AnimeManager;
import com.googlecode.animeclassifier.MoveFileTranslator;
import com.googlecode.animeclassifier.PrintFileTranslator;

public class ClassifyPanel extends JPanel {
	final AnimeManager animeManager;
	private JTextField outputDirectory;
	private JTextField outputFormat;
	private JTextArea result;
	private JScrollPane resultScroll;

	/**
	 * @param animeManager
	 */
	public ClassifyPanel(final AnimeManager animeManager) {
		super();
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.animeManager = animeManager;
		
		JPanel panel = new JPanel();
		panel.add(new JLabel("Output Directory: "));
		panel.add(outputDirectory = new JTextField(animeManager.getRoot().getAbsolutePath(), 60));
		this.add(panel);
		
		panel = new JPanel();
		panel.add(new JLabel("Output Format: "));
		panel.add(outputFormat = new JTextField("%YEAR%/%SEASON/%NAME%", 60));
		this.add(panel);
		
		this.add(new JLabel("Result preview: "));
		this.add(resultScroll = new JScrollPane(result = new JTextArea()));
		result.setRows(50);
		
		panel = new JPanel();
		JButton button = new JButton("Preview");
		button.setHorizontalAlignment(JButton.RIGHT);
		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				String str = null;
				
				for(Anime a: animeManager.getAnimes()) {
					if(
							!(str = a.moveAnime("%YEAR%/%SEASON%/%NAME%", animeManager.getRoot(), new PrintFileTranslator())).equals("")
					) {
						result.append(str);
						result.append("\n");
					}
				}
			}
			
		});
		
		JButton button2 = new JButton("DO IT");
		button2.setHorizontalAlignment(JButton.RIGHT);
		button2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				for(Anime a: animeManager.getAnimes()) {
					a.moveAnime("%YEAR%/%SEASON%/%NAME%", animeManager.getRoot(), new MoveFileTranslator());
				}
			}
			
		});
		panel.add(button);
		panel.add(button2);
		this.add(panel);
	}
	
	
}
